<template>
  <layout-box one="我的快递" two="快递录入">
    <el-row :gutter="20">
      <!-- <el-col :span="8">
        <el-input placeholder="请输入内容" clearable>
          <el-button icon="el-icon-search"></el-button>
        </el-input>
      </el-col> -->
      <el-col :span="4">
        <el-button type="primary" @click="addressVisible = true"
          >快递录入</el-button
        >
      </el-col>
    </el-row>
    <el-table :data="orderList" border stripe>
      <el-table-column type="index"></el-table-column>
      <el-table-column label="寄件人姓名" prop="fromName"></el-table-column>
      <el-table-column label="寄件人地址" prop="fromCity"></el-table-column>
      <el-table-column label="收件人姓名" prop="toName"></el-table-column>
      <el-table-column label="收件人地址" prop="toCity"></el-table-column>
      <el-table-column label="物流信息" prop="currentArea">
        <template slot-scope="scope">
          <span v-if="scope.row.currentArea == null">等待物流更新</span>
          <span v-else>{{ scope.row.currentArea }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 0">已揽收</span>
          <span v-if="scope.row.status == 1">运输中</span>
          <span v-if="scope.row.status == 2">已签收</span>
          <span v-if="scope.row.status == 3">等待揽收</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" v-if="personInfo != 1">
        <template slot-scope="scope">
          <el-button
            class="el-icon-edit"
            type="primary"
            size="mini"
            @click="showBox(scope.row)"
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="deleteManger(scope.row.flowId)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[1, 10, 20, 50]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>

    <!-- 录入快递弹出框 -->
    <el-dialog
      title="录入快递"
      :visible.sync="addressVisible"
      width="40%"
      @close="handleClose"
    >
      <el-form
        :model="express"
        :rules="addressFormRules"
        ref="addressFormRef"
        label-width="120px"
      >
        <el-form-item label="寄件人姓名" prop="fromName">
          <el-input v-model="express.fromName"></el-input>
        </el-form-item>
        <el-form-item label="寄件地址" prop="fromCity">
          <el-input v-model="express.fromCity"></el-input>
        </el-form-item>
        <el-form-item label="寄件人手机号" prop="fromPhone">
          <el-input v-model="express.fromPhone"></el-input> </el-form-item
        ><el-form-item label="收件人姓名" prop="toName">
          <el-input v-model="express.toName"></el-input>
        </el-form-item>
        <el-form-item label="收件地址" prop="toCity">
          <el-input v-model="express.toCity"></el-input>
        </el-form-item>
        <el-form-item label="收件人手机号" prop="toPhone">
          <el-input v-model="express.toPhone"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addressVisible = false">取 消</el-button>
        <el-button type="primary" @click="expressEntry">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改快递信息弹出框 -->
    <el-dialog
      title="录入快递"
      :visible.sync="eidtVisible"
      width="40%"
      @close="handleClose"
    >
      <el-form
        :model="express"
        :rules="addressFormRules"
        ref="addressFormRef"
        label-width="120px"
      >
        <el-form-item label="寄件人姓名" prop="fromName">
          <el-input v-model="express.fromName"></el-input>
        </el-form-item>
        <el-form-item label="寄件地址" prop="fromCity">
          <el-input v-model="express.fromCity"></el-input>
        </el-form-item>
        <el-form-item label="寄件人手机号" prop="fromPhone">
          <el-input v-model="express.fromPhone"></el-input> </el-form-item
        ><el-form-item label="收件人姓名" prop="toName">
          <el-input v-model="express.toName"></el-input>
        </el-form-item>
        <el-form-item label="收件地址" prop="toCity">
          <el-input v-model="express.toCity"></el-input>
        </el-form-item>
        <el-form-item label="收件人手机号" prop="toPhone">
          <el-input v-model="express.toPhone"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addressVisible = false">取 消</el-button>
        <el-button type="primary" @click="editExpressEntry">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 物流进度对话框 -->
    <el-dialog
      title="物流进度"
      :visible.sync="showProgressVisible"
      width="50%"
      @close="handleClose"
    >
      <el-form
        :model="progressInfo"
        :rules="addressFormRules"
        ref="addressFormRef"
        label-width="100px"
      >
        <el-form-item label="寄件人" prop="fromName">
          <el-input v-model="progressInfo.fromName"></el-input>
        </el-form-item>
        <el-form-item label="寄件地址" prop="fromCity">
          <el-input v-model="progressInfo.fromCity"></el-input> </el-form-item
        ><el-form-item label="收件人" prop="context">
          <el-input v-model="progressInfo.context"></el-input>
        </el-form-item>
        <el-form-item label="收件地址" prop="context">
          <el-input v-model="progressInfo.context"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addressVisible = false">取 消</el-button>
        <el-button type="primary" @click="expressEntry">确 定</el-button>
      </span>
    </el-dialog>
  </layout-box>
</template>

<script>
export default {
  data() {
    // 验证手机号
    var checkphoneNo = (rule, value, cb) => {
      const regphoneNo = /^1[34578]\d{9}$/;
      if (regphoneNo.test(value)) {
        return cb();
      }
      //返回一个错误提示
      cb(new Error("请输入合法的手机号"));
    };
    return {
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      total: 0,
      orderList: [],
      eidtVisible: false,
      addressVisible: false,
      // 默认物流对话框是关闭状态的
      showProgressVisible: false,
      addressForm: {
        address1: [],
        address2: "",
      },
      addressFormRules: {
        fromName: [
          { required: true, message: "请输入寄件人姓名", trigger: "blur" },
        ],
        fromCity: [
          { required: true, message: "请输入寄件地址", trigger: "blur" },
        ],
        fromPhone: [
          { required: true, message: "请输入寄件人手机号", trigger: "blur" },
          {
            validator: checkphoneNo,
            message: "手机号格式不正确,请重新输入",
            trigger: "blur",
          },
        ],
        toName: [
          { required: true, message: "请输入收件人姓名", trigger: "blur" },
        ],
        toCity: [
          { required: true, message: "请输入收件地址", trigger: "blur" },
        ],
        toPhone: [
          { required: true, message: "请输入收件人手机号", trigger: "blur" },
          {
            validator: checkphoneNo,
            message: "手机号格式不正确,请重新输入",
            trigger: "blur",
          },
        ],
      },
      //   物流信息
      progressInfo: [],
      express: {
        fromName: "",
        fromCity: "",
        fromPhone: "",
        toName: "",
        toCity: "",
        toPhone: "",
      },
      personInfo: "",
    };
  },
  created() {
    this.personInfo = window.sessionStorage.getItem("personInfo");
    this.getOrderList();
  },
  methods: {
    // 录入快递信息
    async expressEntry() {
      const { data: res } = await this.$http.post("/express/add", {
        fromName: this.express.fromName,
        fromCity: this.express.fromCity,
        fromPhone: this.express.fromPhone,
        toName: this.express.toName,
        toCity: this.express.toCity,
        toPhone: this.express.toPhone,
      });
      if (res.code != 200) {
        return this.$message.error("寄件失败");
      }
      this.$message.success("寄件成功");
      this.addressVisible = false;
      this.getOrderList();
    },
    // 修改快递信息
    async editExpressEntry() {
      const { data: res } = await this.$http.post("/express/update", {
        flowId: this.express.flowId,
        fromName: this.express.fromName,
        fromCity: this.express.fromCity,
        fromPhone: this.express.fromPhone,
        toName: this.express.toName,
        toCity: this.express.toCity,
        toPhone: this.express.toPhone,
      });
      if (res.code != 200) {
        return this.$message.error("寄件失败");
      }
      this.$message.success("寄件成功");
      this.eidtVisible = false;
      this.getOrderList();
    },
    // 获取快递信息
    async getOrderList() {
      // 查询所有
      if (window.sessionStorage.getItem("personInfo") != 1) {
        const { data: res } = await this.$http.post("/express/getAll", {
          query: this.queryInfo.query,
          pagenum: this.queryInfo.pagenum,
          pagesize: this.queryInfo.pagesize,
        });
        this.orderList = res.data.expressList;
        this.total = res.data.count;
      } else {
        //查询自己
        const { data: res } = await this.$http.get("/express/getByUserName");
        this.orderList = res.data;
        console.log(res);
      }
    },
    // 删除快件
    async deleteManger(id) {
      const { data: res } = await this.$http.delete("/express/delete/" + id);
      if (res.code != 200) {
        return this.$message.error("删除快件失败");
      } else {
        this.getOrderList();
      }
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getOrderList();
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getOrderList();
    },
    showBox(data) {
      this.eidtVisible = true;
      this.express = data;
    },
    handleClose() {
      this.$refs.addressFormRef.resetFields();
    },
    async showProgressBox() {
      const { data: res } = await this.$http.get(`/kuaidi/1106975712662`);
      if (res.meta.status != 200) {
        return this.$Message.error("获取物流信息失败");
      }
      console.log(res.data);
      this.progressInfo = res.data;
      this.showProgressVisible = true;
    },
  },
};
</script>

<style lang='less' scoped>
</style>
